/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.football.lottery.util;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author I am god
 */
public class ResultCombination
{
//  public static void main(String[] args)
//  {
//    List data = new ArrayList();
//    data.add("abc");
//    data.add("def");
//    data.add("ghi");
//    data.add("jkl");
//    ResultCombination combination = new ResultCombination();
//    List resultList = combination.getResultCombination(data);
//    System.out.println(resultList.size());
//    for (Object result : resultList)
//    {
//      System.out.println(result.toString());
//    }
//  }

  public static List<List<String>> getAllResultCombination(List<List<String>> datas)
  {
    List<List<String>> allResults = new ArrayList<List<String>>();
    for (List<String> data : datas)
    {
      allResults.add(ResultCombination.getResultCombination(data));
    }
    return allResults;
  }

  public static List<String> getResultCombination(List<String> data)
  {
    List<String> resultList = new ArrayList<String>();
    StringBuilder result = new StringBuilder();
    combiner(data, resultList, result);
    return resultList;
  }

  private static void combiner(List<String> data, List<String> resultList, StringBuilder temp)
  {
    String resultStr = "";
    if (data.size() == 1)
    {
      resultStr = (String) data.get(0);
      for (int index = 0; index < resultStr.length(); index++)
      {
        StringBuilder result = new StringBuilder(temp);
        result.append(resultStr.charAt(index));
        resultList.add(result.toString());
      }
    }
    if (data.size() > 1)
    {
      resultStr = (String) data.get(0);
      for (int index = 0; index < resultStr.length(); index++)
      {
        StringBuilder result = new StringBuilder(temp);
        result.append(resultStr.charAt(index));
        List newData = new ArrayList(data);

        if (newData.size() > 0)
        {
          newData.remove(0);
        }
        
        combiner(newData, resultList, result);
        result = new StringBuilder();
      }
    }
  }
}
